#include <QApplication>
#include <QMainWindow>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <QTextEdit>
#include <QMenuBar>
#include <QAction>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow mainWindow;
    mainWindow.setWindowTitle("QMdiArea 示例");

    // 创建 MDI 区域
    QMdiArea *mdiArea = new QMdiArea();
    mainWindow.setCentralWidget(mdiArea);

    // 创建菜单栏和新建子窗口动作
    QMenuBar *menuBar = mainWindow.menuBar();
    QMenu *fileMenu = menuBar->addMenu("文件");
    QAction *newAction = new QAction("新建子窗口", &mainWindow);
    fileMenu->addAction(newAction);

    // 新建子窗口的槽函数
    QObject::connect(newAction, &QAction::triggered, [&]() {
        // 创建一个子窗口
        QMdiSubWindow *subWindow = new QMdiSubWindow();
        QTextEdit *textEdit = new QTextEdit();
        subWindow->setWidget(textEdit);
        subWindow->setWindowTitle("子窗口");
        mdiArea->addSubWindow(subWindow);
        subWindow->show();
    });

    mainWindow.resize(800, 600);
    mainWindow.show();

    return app.exec();
}
